/**
 * @author Vanin Yuri
 * Date: 28.03.2009
 */
package univer.oko.olap.report;

import univer.oko.utilities.CDUtilities;
import univer.oko.utilities.NumberUtilities;
import univer.oko.database.rmi.RMILogger;
import univer.oko.excel.ExcelReportWriter;
import univer.oko.query.QueryResult;
import univer.oko.report.HTMLStringFormatter;

import java.io.FileWriter;
import java.io.IOException;

public class ReportWriter {
    private static ReportWriter ourInstance = new ReportWriter();

    public static ReportWriter getInstance() {
        return ourInstance;
    }

    private ReportWriter() {
    }

    public void createHTMLReport(ReportData data, String fileName, String title) {
        try
        {
            FileWriter fw = new FileWriter(fileName);

            fw.write("<HTML>\n" +
                    "<HEAD>\n" +
                    "<STYLE>\n" +
                    "TABLE {border: thin solid; border-color: #000000; font-family: Tahoma;}\n" +
                    "TD {padding-left: 15px; padding-right: 15px; font-family: Tahoma;}\n" +
                    ".title {font-family: Tahoma; font-size: 10pt; font-weight: bold;}\n" +
                    "</STYLE>\n" +
                    "</HEAD>\n" +
                    "<BODY>\n" +
                    "<SPAN class=title>" + HTMLStringFormatter.format(title, "BOLD", "CENTER") + "</SPAN>\n" +
                    "<TABLE align=\"center\" rules='all' cellspacing='0' cellpadding='3'>\n" +
                    "<TR bgcolor=lightgrey>\n");

            // print columns
            for(int i=0; i<data.getColumnCount(); i++)
                fw.write("<TH>" + data.getColumn(i) + "</TH>\n");
            fw.write("</TR>\n");

            String printedValue;
            String style;
            //print rows
            for(int i=0; i<data.getRowCount(); i++) {
                if (data.getValueAt(i,0).toString().indexOf('?')>-1)
                    continue;
                fw.write("<TR>\n");
                for (int j=0; j<data.getColumnCount(); j++) {
                    int rowspan = 1;
                    if (!data.getValueAt(i,j).equals(" ") && i!=data.getRowCount()) {
                        String v = data.getValueAt(i,j).toString();
                        for(int k=i+1; k<data.getRowCount(); k++)
                            if (data.getValueAt(k,j).equals(v)) {
                                data.setValueAt(k,j," ");
                                rowspan++;
                            }
                            else
                                break;
                    }
                    if (data.getValueAt(i,j).equals(" "))
                        continue;

                    if (data.getTypes().get(j) == QueryResult.DataType.DOUBLE) {
                        printedValue = NumberUtilities.formatDouble2Print((Double)data.getValueAt(i,j));
                        style = " align=right";
                    }
                    else {
                        printedValue = data.getValueAt(i,j).toString();
                        style = "";
                    }

                    if (rowspan == 1)
                        fw.write("<TD" + style + ">" + printedValue + "</TD>\n");
                    else
                        fw.write("<TD rowspan=" + rowspan + " style='vertical-align: top;'>" + printedValue + "</TD>\n");
                }
                fw.write("</TR>\n");
            }

            fw.write("</TABLE>\n" +
                     "</BODY>\n" +
                     "</HTML>");

            fw.close();
        }catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    public void runHTMLReport(ReportData data, String fileName, String title) {
        createHTMLReport(data,fileName,title);
        runReport(CDUtilities.BROWSER_PATH + " \"file:" + fileName + "\"");
    }

    public void runExcelReport(ReportData data, String fileName) {
        ExcelReportWriter.createReport("",fileName,data.getHeaders(),data.getData(),data.getTypes());
    }

    protected void runReport(String filePath) {
        Runtime rt = Runtime.getRuntime();
        try {
            rt.exec(filePath);
        }
        catch (IOException e) {
            RMILogger.ClientLogAndMessage(e);
        }
    }
}
